<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<!--修改模态框信息-->
<div class="modal fade" data-keyboard="false" id="modal-update-Stock">
    <div class="modal-dialog" style="width:1000px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h3 class="modal-title">库存信息</h3>
            </div>
            <div class="modal-body">
                <form id="projectForm" class="form-horizontal">
                    <input type="hidden" id="adjId" name="adjId">
                    <input type="hidden" name="matFormulaType">
                    <input type="hidden" name="matHeight">
                    <input type="hidden" name="matLength">
                    <input type="hidden" name="matWidth">
                    <input type="hidden" name="matSpecId">
                    <div class="row">
                        <div class="col-md-8 col-md-offset-2">
                            <div class="form-group">
                                <label class="col-sm-2 text-right">物料描述 </label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="adjMatDesc"
                                           style="width: 430px" placeholder="" readonly>
                                </div>
                                <div class="col-sm-2">
                                    <button type="button" class="btn btn-info pull-right"
                                            id="btn-chooseMaterial" onclick="openDailyDescAdd('')">
                                        <span class="glyphicon glyphicon-search"></span>选 择
                                    </button>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 text-right">在库数量</label>
                                <div class="col-sm-2">
                                    <input type="text" class="form-control" name="adjAfterNum" placeholder=""
                                           style="width: 190px">
                                </div>
                                <label class="col-sm-4 text-right">在库重量</label>
                                <div class="col-sm-3">
                                    <input type="text" class="form-control" name="adjAfterWeight" placeholder=""
                                           style="width: 190px" readonly>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 text-right">单价</label>
                                <div class="col-sm-2">
                                    <input type="text" class="form-control" name="adjPrice"
                                           style="width: 190px" placeholder="">
                                </div>
                                <label class="col-sm-4 text-right">金额</label>
                                <div class="col-sm-3">
                                    <input type="text" class="form-control" name="adjAmount"
                                           style="width: 190px" placeholder="" readonly>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 text-right">可用数量</label>
                                <div class="col-sm-2">
                                    <input type="text" class="form-control" name="adjAfterAvaNum"
                                           style="width: 200px" readonly>
                                </div>
                                <label class="col-sm-4 text-right">可用重量</label>
                                <div class="col-sm-3">
                                    <input type="text" class="form-control" name="adjAfterAvaWeight"
                                           style="width: 195px" readonly>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 text-right">跟踪号</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" name="adjTrackCode" placeholder="" readonly>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 text-right">在库类型</label>
                                <div class="col-sm-10">
                                    <select name="adjInType" class="form-control" disabled>
                                        <option value="1">库存</option>
                                        <option value="2">项目</option>
                                    </select>
                                    <!--<input type="text" class="form-control" name="ityInType"  placeholder="" readonly>-->
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 text-right">特殊说明</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" name="adjSpec" placeholder="" readonly>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 text-right">项目编号</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" name="adjProjectCode" placeholder=""
                                           readonly>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 text-right">项目名称</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" name="adjProjectName" placeholder=""
                                           readonly>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 text-right">调整原因</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" name="adjReason" placeholder="">
                                </div>
                            </div>

                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-update-project">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<!--调整选择物料描述模态框-->
<div class="modal fade" data-backdrop="static" data-keyboard="false" id="modal-mate-project">
    <div class="modal-dialog" style="width:1350px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">物料基础&nbsp;&nbsp;&nbsp;&nbsp;<span id="projectInfo"
                                                                          style="font-size: 10px"></span>
                </h4>
            </div>
            <div class="modal-body">
                <form id="queryMateEnquiry">
                    <input type="hidden" id="matId" name="matId">
                    <div class="row">
                        <div class="form-inline">
                            <div class="form-group" style="margin-right: 10px;">
                                <label>类型</label>
                                <input type="text" id="matTypeId" name="matTypeId"
                                       class="form-control"
                                       placeholder="请输入原料类型">
                            </div>
                            <div class="form-group" style="margin-right: 10px;">
                                <label>材质</label>
                                <input type="text" id="matQualityId" name="matQualityId"
                                       class="form-control"
                                       placeholder="请输入原料材质">
                            </div>
                            <div class="form-group" style="margin-right: 10px;">
                                <label>规格</label>
                                <input type="text" id="matSpecId" name="matSpecId"
                                       class="form-control"
                                       placeholder="请输入原料规格">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-inline">
                            <div class="form-group" style="margin-right: 10px; margin-top:20px;">
                                <label>宽度</label>
                                <input type="number" step="0.1" id="matWidth"
                                       name="matWidth" class="form-control"
                                       placeholder="请输入原料宽度">
                            </div>
                            <div class="form-group" style="margin-right: 10px; margin-top:20px;">
                                <label>长度</label>
                                <input type="number" step="0.1" id="matLength"
                                       name="matLength" class="form-control"
                                       placeholder="请输入原料长度">
                            </div>
                            <div class="form-group" style="margin-right: 10px; margin-top:20px;">
                                <label>米重/厚度</label>
                                <input type="number" step="0.1" id="matHeight"
                                       name="matHeight" class="form-control"
                                       placeholder="请输入原料米重/厚度">
                            </div>
                            <div class="form-group" style="margin-right: 10px; margin-top:20px;">
                                <label>原料分类</label>
                                <select id="materialKind" name="materialKind"
                                        class="form-control">
                                    <option value="板材">板材</option>
                                    <option value="型材">型材</option>
                                    <option value="" selected="selected">全部</option>
                                </select>
                            </div>
                            <button type="button" class="btn btn-success" id="btn_materialSearch"
                                    style="margin-top: 20px;">
                                <span class="Bold">搜索</span>
                            </button>
                        </div>
                    </div>
                </form>
                <table id="MateEnquiry-table1"></table>
            </div>
        </div>
    </div>
</div>
<!--加载首页-->
<section class="content-header">
    <h1>库存调整申请</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>物料描述</label>
                                    <input type="text" id="adjMatDesc" name="adjMatDesc"
                                           class="form-control"
                                           placeholder="请输入物料描述">
                                </div>
                                <div class="form-group">
                                    <label>项目编号</label>
                                    <input type="text" id="adjProjectCode" name="adjProjectCode"
                                           class="form-control"
                                           placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label>项目名称</label>
                                    <input type="text" id="adjProjectName" name="adjProjectName"
                                           class="form-control"
                                           placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label>调整原因</label>
                                    <input type="text" id="adjReason" name="adjReason"
                                           class="form-control"
                                           placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label>跟踪号</label>
                                    <input type="text" id="adjTrackCode" name="adjTrackCode"
                                           class="form-control"
                                           placeholder="请输入跟踪号">
                                </div>
                                <div class="form-group">
                                    <label>
                                        状态
                                    </label>
                                    <select id="adjStatus" name="adjStatus" class="form-control">
                                        <option value="2" selected="selected">未提交</option>
                                        <option value="3">待审核</option>
                                        <option value="4">审核通过</option>
                                        <option value="5">审核不通过</option>
                                        <option value="">全部</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>
                                        在库类型
                                    </label>
                                    <select id="adjInType" name="adjInType" class="form-control">
                                        <option value="" selected="selected">全部</option>
                                        <option value="1">库存</option>
                                        <option value="2">项目</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>时间</label>
                                    <input type="text" class="form-control datepicker" name="startTime"
                                           readonly="readonly"/>
                                    <span>至</span>
                                    <input type="text" class="form-control datepicker" name="endTime"
                                           readonly="readonly"/>
                                </div>
                                <button type="button" class="btn btn-success" id="btn_search">搜索</button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="row" id="toolbar">
                    <button shiro:hasPermission="matinv:matadjlist:edit" type="button"
                            class="btn btn-success"
                            id="btn-togetherCommit">
                        批量提交
                    </button>
                    <a shiro:hasPermission="matinv:matadjlist:edit"
                       th:href="@{/material/matInvManage/rawMatJustApply/create}"
                       class="btn btn-info">调整库存</a>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    var table, table1;

    /**
     * 根据id回显修改数据
     */
    function openUpdateStockModal(adjId) {
        js.post({
            url: ctx + "material/matInvManage/rawMatJustApply/adjId",
            data: {
                adjId: adjId
            },
            success: function (result) {
                $("#projectForm").fillData(result);
                js.modal.open("modal-update-Stock");
                js.modal.closeLoading();
            }
        })
    }

    /**
     * 保存变更物料描述
     */
    $("#btn-update-project").click(function () {
        if ($("#adjId").val()) {
            url = ctx + "material/matInvManage/rawMatJustApply/update";
        }
        js.validSubmit({
            formId: "projectForm",
            url: url,
            data: new FormData($("#projectForm")[0]),
            success: function (result) {
                js.modal.success(result.msg);
                js.table.refresh(table);
                js.modal.hide("modal-update-Stock");
                js.modal.closeLoading();
            }
        });
    });

    /**
     * 根据ID删除
     */
    function delTaskDaily(adjId) {
        js.modal.confirm("确定删除此数据吗？", function () {
            js.post({
                url: ctx + "material/matInvManage/rawMatJustApply/del",
                data: {
                    adjId: adjId

                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();

                }
            })
        })
    }

    //根据id提交待审核数据
    function and(adjId) {
        js.modal.confirm("确定提交吗？", function () {
            js.post({
                url: ctx + "material/matInvManage/rawMatJustApply/and",
                data: {
                    adjId: adjId
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();

                }
            })
        })
    }


    // 取得原料行数据
    function getMaterial(d) {
        $("#projectForm [name='adjMatDesc']").val(d.matNameDesc);
        $("#projectForm [name='matFormulaType']").val(d.matFormulaType);
        $("#projectForm [name='matHeight']").val(d.matHeight);
        $("#projectForm [name='matLength']").val(d.matLength);
        $("#projectForm [name='matWidth']").val(d.matWidth);
        $("#projectForm [name='matSpecId']").val(d.matSpecId);
        js.modal.hide("modal-mate-project");
        js.table.refresh(table1);
    }

    //修改计算在库重量
    $("#projectForm [name='adjAfterNum']").change(function () {
        var materialSpec = $("#projectForm [name='matSpecId']").val();
        var materialLength = $("#projectForm [name='matLength']").val();
        var materialWidth = $("#projectForm [name='matWidth']").val();
        var materialHeight = $("#projectForm [name='matHeight']").val();
        var ityNum = $("#projectForm [name='adjAfterNum']").val();
        var matFormulaType = $("#projectForm [name='matFormulaType']").val();
        var totalWeight = returnMaterialWeight(materialSpec, materialLength, materialWidth, materialHeight, ityNum, matFormulaType);
        $("#projectForm [name='adjAfterWeight']").val(totalWeight);

    });

    //修改时计算单位与金额
    $("#projectForm [name='adjPrice']").change(function () {
        var ityWeight = $("#projectForm [name='adjAfterWeight']").val();
        var ityPrice = $("#projectForm [name='adjPrice']").val();
        // var adjAmount = $("#projectForm [name='adjAmount']").val();
        var totalAmount = returnMaterialAmount(ityWeight, ityPrice);
        $("#projectForm [name='adjAmount']").val(totalAmount);
    });

    //选择物理信息加载页面
    function openDailyDescAdd() {

        js.modal.open("modal-mate-project");
        table1 = js.table.init({
            id: "MateEnquiry-table1",
            queryForm: "queryMateEnquiry",
            url: ctx + "material/matInvManage/rawMatJustApply/MateEnquiryList",
            columns: [
                {
                    title: '序号', field: 'matId', width: '40',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: '编码', field: 'matCode', width: '80'},
                {title: '分类', field: 'materialKind', width: '150'},
                {title: '类型', field: 'matTypeId', width: '118'},
                {title: '材质', field: 'matQualityId', width: '100'},
                {title: '规格', field: 'matSpecId', width: '100'},
                {title: '宽度', field: 'matWidth', width: '100'},
                {title: '长度', field: 'matLength', width: '100'},
                {title: '米重/厚度', field: 'matHeight', width: '100'},
                {
                    title: '操作', width: '65',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push("<a style='margin-right: 5px' class='btn btn-danger btn-xs intent-del' href='javascript:void(0);' onclick='getMaterial(" + JSON.stringify(row) + " )'>选择</a>");
                        return actions.join('');
                    }
                }
            ]
        })
    }


    $(function () {
        /**
         * 加载列表
         */
        table = js.table.init({
            url: ctx + "material/matInvManage/rawMatJustApply/onelist",
            pageSize: 300,
            onLoadSuccess: function (data) {
                var dataSource = $('#bootstrap-table').bootstrapTable('getData', true);
                mergeCells(dataSource, "cellMergeFlg", 1, $('#bootstrap-table')); //合并单元格
            },
            rowStyle: function (row, index) {
                if (row.adjAfterItyId === 0) {
                    return {css: {"border-top-color": "#333"}}
                } else {
                    return {css: {"border-bottom-color": "#333", "border-bottom": "solid 1px"}}
                }
            },
            columns: [
                {
                    field: "checked",
                    title: '全选',
                    checkbox: true,
                    formatter: function (value, row, index) {
                        if (row.adjStatus === 2) {
                            return {
                                disabled: false
                            }
                        } else {
                            return {
                                disabled: true
                            }
                        }
                    }
                },
                /*{
                    title: 'color', field: 'adjAfterItyId', width: '80'
                },*/
                {title: '调整数量', field: 'adjNum', width: '50'},
                {title: '调整重量', field: 'adjWeight', width: '50'},
                {title: '跟踪号', field: 'adjTrackCode', width: '60'},
                {title: '物料描述', field: 'adjMatDesc', width: '200'},
                {title: '项目编号', field: 'adjProjectCode', width: '150'},
                {title: '项目名称', field: 'adjProjectName', width: '200'},
                {title: '特殊说明', field: 'adjSpec', width: '100'},
                {
                    title: '在库类型', field: 'adjInType', width: '50',
                    formatter: function (value, row, index) {
                        if (value === 1) {
                            return '库存';
                        } else if (value === 2) {
                            return '项目';
                        }
                    }
                },
                {title: '调整原因', field: 'adjReason', width: '100'},
                {title: '申请人', field: 'adjPerson', width: '60'},
                {title: '申请日期', field: 'adjDate', width: '120'},
                {
                    title: '状态', field: 'adjStatus', width: '100',
                    formatter: function (value, row, index) {
                        if (value === 2) {
                            return '<span class="badge bg-gray">未提交</span> ';
                        } else if (value === 3) {
                            return '<span class="badge bg-yellow">待审核</span> ';
                        } else if (value === 4) {
                            return '<span class="badge bg-green">审核通过</span> ';
                        } else if (value === 5) {
                            return '<span class="badge bg-red">审核不通过</span> ';
                        }
                    }
                },
                {
                    title: '操作', field: 'adjOdaId', width: '140',
                    formatter: function (value, row, index) {
                        var html = [];
                        //数量修改判断按键显隐
                        if (row.adjStatus == 2 && row.adjBeAfFlag == null && row.adjOditStatus == null) {
                            html.push('<a class="btn btn-info btn-xs"  href="' + ctx + 'material/matInvManage/rawMatJustApply/aaa/' + row.adjId + '" > 查看</a> ');
                            html.push('<a class="btn btn-xs btn-warning" href="javascript:void(0)" onclick="openUpdateStockModal(' + row.adjId + ')"> 修改</a> ');
                            html.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="delTaskDaily(' + row.adjId + ')">删除</a> ');
                            html.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="and(' + row.adjId + ')"> 提交</a>');
                        } else if (row.adjStatus == 3 && row.adjBeAfFlag == null && row.adjOditStatus == null) {
                            html.push('<a class="btn btn-info btn-xs" href="' + ctx + 'material/matInvManage/rawMatJustApply/aaa/' + row.adjId + '" > 查看</a> ');
                        }
                        //新增库存判断按键显隐
                        if (row.adjOditStatus == 1 && row.adjType == 1 && row.adjStatus == 2) {
                            html.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="delTaskDaily(' + row.adjId + ')">删除</a> ');
                            html.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="and(' + row.adjId + ')"> 提交</a>');
                        } else if (row.adjOditStatus == 1 && row.adjStatus == 3) {
                            return '';
                        } else if (row.adjOditStatus == 1 && row.adjStatus == 4) {
                            return '';
                        }
                        //修改物料描述判断按键显隐
                        if (row.adjStatus == 2 && row.adjOditStatus == 2 && row.adjBeAfFlag == 1) {
                            html.push('<a class="btn btn-info btn-xs"   href="' + ctx + 'material/matInvManage/rawMatJustApply/aaa/' + row.adjId + '" > 查看</a> ');
                            html.push('<a class="btn btn-xs btn-warning"  href="javascript:void(0)" onclick="openUpdateStockModal(' + row.adjId + ')"> 修改</a> ');
                            html.push('<a class="btn btn-danger btn-xs"  href="javascript:void(0)" onclick="delTaskDaily(' + row.adjId + ')">删除</a> ');
                            html.push('<a class="btn btn-success btn-xs"  href="javascript:void(0)" onclick="and(' + row.adjId + ')"> 提交</a>');
                        } else if (row.adjStatus == 3 && row.adjOditStatus == 2 && row.adjBeAfFlag == 1) {
                            html.push('<a class="btn btn-info btn-xs" href="' + ctx + 'material/matInvManage/rawMatJustApply/aaa/' + row.adjId + '" > 查看</a> ');
                        }
                        //判断审核通过或不通过按钮显隐
                        if (row.adjStatus == 4) {
                            html.push('<a class="btn btn-info btn-xs" href="' + ctx + 'material/matInvManage/rawMatJustApply/aaa/' + row.adjId + '" > 查看</a> ');
                        }
                        if (row.adjStatus == 5) {
                            html.push('<a class="btn btn-info btn-xs" href="' + ctx + 'material/matInvManage/rawMatJustApply/aaa/' + row.adjId + '" > 查看</a> ');
                        }
                        return html.join('');
                    }
                }
            ]
        });

        /**
         * 合并单元格
         * @param data  原始数据（在服务端完成排序）
         * @param fieldName 合并属性名称
         * @param colspan   合并列
         * @param target    目标表格对象
         */
        function mergeCells(data, fieldName, colspan, target) {
            var valueArry = new Array();//数组只记录值
            for (var i = 0; i < data.length; i++) {
                for (var prop in data[i]) {
                    if (prop == fieldName) {
                        var key = data[i][prop]
                        valueArry[i] = key;
                        break;
                    }
                }
            }

            var numArry = new Array();//记录值出现的次数  一会就通过循环次数组  合并单元格
            var value = valueArry[0];//初始值
            var num = 0;//记录值出现的次数
            var index1 = 0;//numArry的下标
            for (var i = 0; i <= valueArry.length; i++) {
                if (i == valueArry.length) {//解决最后一次赋值
                    numArry[index1] = num;
                    break;
                }
                if (value == valueArry[i]) {
                    num++;//list下一个值与变量value值相同时，num+1，下标不变
                } else {
                    value = valueArry[i];//如果值不相等就把下一个值   赋值给value变量
                    numArry[index1] = num;//把value变量之前的值 出现的次数记录下来
                    num = 1;// 新的value值出现一次
                    index1++;//下标+1;
                }
            }

            var index = 0;
            var fiedArry = ["checked", "adjStatus", "adjOdaId"];
            for (var i = 0; i < numArry.length; i++) {
                var count = numArry[i] * 1;
                fiedArry.forEach(function (field) {
                    $(target).bootstrapTable('mergeCells', {
                        index: index,
                        field: field,
                        colspan: colspan,
                        rowspan: count
                    });
                });
                index += count;
            }
        }

        /** 批量提交 */
        $("#btn-togetherCommit").click(function () {

            var rows = js.table.selectRows("bootstrap-table");

            // 如果没有选择，显示提示信息
            if (rows.length == 0) {
                js.modal.warning("请至少选择一条明细");
                return false;
            } else {
                var arr = js.table.selectColumns(table, "adjId");
                js.post({
                    url: ctx + "material/matInvManage/rawMatJustApply/togetherAnd",
                    contentType: "application/json",
                    data: JSON.stringify(arr),
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            js.table.refresh(table);
                        } else {
                            js.modal.warning(result.msg);
                            js.table.refresh(table);
                        }
                        js.modal.closeLoading();
                    }
                })
            }
        });

        /**
         * 表格搜索
         */
        $("#btn_search").click(function () {
            js.table.search(table);
        });
        /**
         * 表格搜索
         */
        $("#btn_materialSearch").click(function () {
            js.table.search(table1);
        });

    });
</script>




